package com.ruoyi.system.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.FlowerInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.FlowerCategoryMapper;
import com.ruoyi.system.domain.FlowerCategory;
import com.ruoyi.system.service.IFlowerCategoryService;

import javax.annotation.Resource;

/**
 * 花卉分类管理Service业务层处理
 *
 * @author lwb
 * @date 2024-12-26
 */
@Service
public class FlowerCategoryServiceImpl implements IFlowerCategoryService {
    @Resource
    private FlowerCategoryMapper flowerCategoryMapper;

    /**
     * 查询花卉分类管理
     *
     * @param id 花卉分类管理主键
     * @return 花卉分类管理
     */
    @Override
    public FlowerCategory selectFlowerCategoryById(Long id) {
        return flowerCategoryMapper.selectFlowerCategoryById(id);
    }

    /**
     * 查询花卉分类管理列表
     *
     * @param flowerCategory 花卉分类管理
     * @return 花卉分类管理
     */
    @Override
    public List<FlowerCategory> selectFlowerCategoryList(FlowerCategory flowerCategory) {
        return flowerCategoryMapper.selectFlowerCategoryList(flowerCategory);
    }

    /**
     * 新增花卉分类管理
     *
     * @param flowerCategory 花卉分类管理
     * @return 结果
     */
    @Override
    public int insertFlowerCategory(FlowerCategory flowerCategory) {
        this.check(flowerCategory);
        flowerCategory.setCreateTime(DateUtils.getNowDate());
        return flowerCategoryMapper.insertFlowerCategory(flowerCategory);
    }

    /**
     * 校验名称
     *
     * @param category
     * @return
     */
    public void check(FlowerCategory category) {
        category = flowerCategoryMapper.check(category);
        if (category != null) {
            throw new ServiceException("花卉名称已存在，请检查！");
        }
    }

    /**
     * 已使用的不可删除
     *
     * @param ids
     */
    public void checkDeleted(Long[] ids) {
        List<FlowerInfo> flowerInfoList = flowerCategoryMapper.checkDeleted(ids);
        if (CollUtil.isNotEmpty(flowerInfoList)) {
            throw new ServiceException("花卉已被使用，不可删除！");
        }
    }

    /**
     * 修改花卉分类管理
     *
     * @param flowerCategory 花卉分类管理
     * @return 结果
     */
    @Override
    public int updateFlowerCategory(FlowerCategory flowerCategory) {
        this.check(flowerCategory);
        flowerCategory.setUpdateTime(DateUtils.getNowDate());
        return flowerCategoryMapper.updateFlowerCategory(flowerCategory);
    }

    /**
     * 批量删除花卉分类管理
     *
     * @param ids 需要删除的花卉分类管理主键
     * @return 结果
     */
    @Override
    public int deleteFlowerCategoryByIds(Long[] ids) {
        this.checkDeleted(ids);
        return flowerCategoryMapper.deleteFlowerCategoryByIds(ids);
    }

    /**
     * 删除花卉分类管理信息
     *
     * @param id 花卉分类管理主键
     * @return 结果
     */
    @Override
    public int deleteFlowerCategoryById(Long id) {
        this.checkDeleted(new Long[]{id});
        return flowerCategoryMapper.deleteFlowerCategoryById(id);
    }

    @Override
    public List<FlowerCategory> listAll() {
        return flowerCategoryMapper.listAll();
    }
}
